<?php
namespace backend\filter;


use backend\component\ResultFormat;
use backend\utils\AuthUtil;
use yii\base\ActionFilter;
use yii\helpers\Url;
use yii\web\Request;

class LoginFilter extends ActionFilter
{
    /**
     * 错误信息
     * @var
     */
    public $errorMsg;

    /**
     * 需要排除的URL
     * @var array
     */
    public $exceptUrl;

    public $request;

    public $response;



    public function init()
    {
        if ($this->request === null) {
            $this->request = \Yii::$app->getRequest();
        }
        if ($this->response === null) {
            $this->response = \Yii::$app->getResponse();
        }
    }

    /**
     * 用户权限检查
     * @param \yii\base\Action $action
     * @return mixed
     */
    public function beforeAction($action)
    {
        //如果当前action被排除则直接返回
        if (in_array($action->id,$this->except)) {
            return true;
        }
        //检查是否有权限
        $url = strtolower(trim($action->getUniqueId(),"/"));
        /*if (!AuthUtil::can($url)){
            // 检查用户是否登录
            if (\Yii::$app->user->isGuest) {
                if (\Yii::$app->request->getIsAjax()) {
                    return ResultFormat::error("登录失效,请重新登录!");
                } else {
                    echo '<script type="text/javascript">window.top.location = "/login";</script>';
                    \Yii::$app->end();
                }
            }
        }*/
        return true;
    }


}